﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;

namespace MIT.DivAdmin
{
    public partial class DivAdminMsg : System.Web.UI.Page
    {

        String Invite1;
        String Invite2;
        String Invite3;
        String Sender1;
        String Sender2;
        String Sender3;
        String Division1;
        String Division2;
        String Division3;
        String ImageUser1;
        String ImageUser2;
        String ImageUser3;
        String GlobalUsername;
        String GlobalPosition;
        String GlobalName;

        String Username1;
        String Email1;
        String request1;
        String request2;
        String request3;

        String sub1;
        String sub2;
        String sub3;

        String Username2;
        String Email2;

        String Username3;
        String Email3;

        String Interviewer1;
        String Interviewer2;
        String Interviewer3;

        String InterviewDate;

        int intNo1;
        int intNo2;
        int intNo3;


        String Req1;
        String Req2;
        String Req3;
        String NewsDate1;
        String NewsDate2;
        String NewsDate3;
        String NewsTitle1;
        String NewsTitle2;
        String NewsTitle3;
        String NewsDetails1;
        String NewsDetails2;
        String NewsDetails3;
        protected void Page_Load(object sender, EventArgs e)
        {
            String Name = (String)Session["LoggingInfo"];
            String Position = (String)Session["LoggingPosition"];
            String username = (String)Session["LoggingInfoUsername"];
            GlobalUsername = username;
            GlobalPosition = Position;
            GlobalName = Name;
            LabelDivAdminName.Text = Name;
            LabelDivAdminPosition.Text = "(" + Position + ")";

            ImageProfile.ImageUrl = "../ProfilePicHandler.ashx?uname=" + username;


            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqCom3 = new SqlCommand("spNotifications", con);
                ReqCom3.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", username);
                ReqCom3.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqCom3.ExecuteReader();

                if (drreq3.Read())
                {
                    Invite1 = Convert.ToString(drreq3["REQUEST1"]);
                    Invite2 = Convert.ToString(drreq3["REQUEST2"]);
                    Invite3 = Convert.ToString(drreq3["REQUEST3"]);
                    Sender1 = Convert.ToString(drreq3["SENDER1"]);
                    Sender2 = Convert.ToString(drreq3["SENDER2"]);
                    Sender3 = Convert.ToString(drreq3["SENDER3"]);
                    Division1 = Convert.ToString(drreq3["DIVISION1"]);
                    Division2 = Convert.ToString(drreq3["DIVISION2"]);
                    Division3 = Convert.ToString(drreq3["DIVISION3"]);

                }
            }
            //////////////////////////////////////////Profile Image Extraction//////////////////////////////////
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqUser1 = new SqlCommand("spGetImageUser1", con);
                ReqUser1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@name", Sender1);
                ReqUser1.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqUser1.ExecuteReader();

                if (drreq3.Read())
                {
                    ImageUser1 = Convert.ToString(drreq3["username"]);


                }
            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqUser1 = new SqlCommand("spGetImageUser2", con);
                ReqUser1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@name", Sender2);
                ReqUser1.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqUser1.ExecuteReader();

                if (drreq3.Read())
                {
                    ImageUser2 = Convert.ToString(drreq3["username"]);


                }
            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqUser1 = new SqlCommand("spGetImageUser3", con);
                ReqUser1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@name", Sender3);
                ReqUser1.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader drreq3;

                drreq3 = ReqUser1.ExecuteReader();

                if (drreq3.Read())
                {
                    ImageUser3 = Convert.ToString(drreq3["username"]);


                }
            }

            if (ImageUser1 != null)
            {
                ImageProf1.ImageUrl = "../ProfilePicHandler.ashx?uname=" + ImageUser1;

            }
            if (ImageUser2 != null)
            {
                ImageProf2.ImageUrl = "../ProfilePicHandler.ashx?uname=" + ImageUser2;
            }
            if (ImageUser3 != null)
            {
                ImageProf3.ImageUrl = "../ProfilePicHandler.ashx?uname=" + ImageUser3;
            }
            /////////////////////////////////////////////////////////////////////////////////////////////////////

            LabelSender1.Text = Sender1;
            LabelSender2.Text = Sender2;
            LabelSender3.Text = Sender3;
            LabelInvite1.Text = Invite1;
            LabelInvite2.Text = Invite2;
            LabelInvite3.Text = Invite3;
            LabelDivision1.Text = Division1;
            LabelDivision2.Text = Division2;
            LabelDivision3.Text = Division3;

            if (LabelSender1.Text == "")
            {
                LabelMsgNo.Text = "";
                ImageProf1.Visible = false;
                ImageProf2.Visible = false;
                ImageProf3.Visible = false;
                LabelSender1.Visible = false;
                LabelSender2.Visible = false;
                LabelSender3.Visible = false;
                LabelInvite1.Visible = false;
                LabelInvite2.Visible = false;
                LabelInvite3.Visible = false;
                LabelDivision1.Visible = false;
                LabelDivision2.Visible = false;
                LabelDivision3.Visible = false;
                ButtonReply1.Visible = false;
                ButtonReply2.Visible = false;
                ButtonReply3.Visible = false;


            }
            else if (LabelSender2.Text == "")
            {
                LabelMsgNo.Text = "1";
                ImageProf2.Visible = false;
                ImageProf3.Visible = false;
                LabelSender2.Visible = false;
                LabelSender3.Visible = false;
                LabelInvite2.Visible = false;
                LabelInvite3.Visible = false;
                LabelDivision2.Visible = false;
                LabelDivision3.Visible = false;
                ButtonReply2.Visible = false;
                ButtonReply3.Visible = false;
            }
            else if (LabelSender3.Text == "")
            {
                LabelMsgNo.Text = "2";
                ImageProf3.Visible = false;
                LabelSender3.Visible = false;
                LabelInvite3.Visible = false;
                LabelDivision3.Visible = false;
                ButtonReply3.Visible = false;
            }
            else
            {
                LabelMsgNo.Text = "3";
            }

            ///////////////////Notification Number//////////////////////////////////////////////////
            

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqMsgNo = new SqlCommand("spMsgCounter", con);
                ReqMsgNo.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", username);
                ReqMsgNo.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader dr;

                dr = ReqMsgNo.ExecuteReader();

                if (dr.Read())
                {
                    Req1 = Convert.ToString(dr["REQUEST1"]);
                    Req2 = Convert.ToString(dr["REQUEST2"]);
                    Req3 = Convert.ToString(dr["REQUEST3"]);
                }

            }
            if (Req1 == "")
            {
                LabelMsgNo.Text = "";

            }
            else if (Req2 == "")
            {
                LabelMsgNo.Text = "1";
            }
            else if (Req3 == "")
            {
                LabelMsgNo.Text = "2";
            }
            else
            {
                LabelMsgNo.Text = "3";
            }


            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews1", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News1 = new SqlParameter("@News1", 1);
                GetNews.Parameters.Add(News1);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate1 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle1 = Convert.ToString(dr["Title"]);
                    NewsDetails1 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews2", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News2 = new SqlParameter("@News2", 2);
                GetNews.Parameters.Add(News2);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate2 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle2 = Convert.ToString(dr["Title"]);
                    NewsDetails2 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews3", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News3 = new SqlParameter("@News3", 3);
                GetNews.Parameters.Add(News3);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate3 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle3 = Convert.ToString(dr["Title"]);
                    NewsDetails3 = Convert.ToString(dr["Details"]);
                }

            }



            LabelDetails1.Text = NewsDetails1;
            LabelDetails2.Text = NewsDetails2;
            LabelDetails3.Text = NewsDetails3;

            LabelNews1.Text = NewsTitle1;
            LabelNews2.Text = NewsTitle2;
            LabelNews3.Text = NewsTitle3;

            LabelDate1.Text = NewsDate1;
            LabelDate2.Text = NewsDate2;
            LabelDate3.Text = NewsDate3;
        }
        protected void ButtonLogout_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Login/SignIn.aspx");
        }

        protected void ButtonReply1_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {

                SqlCommand ReqCom1 = new SqlCommand("spRequestchecker1", con);
                ReqCom1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName1 = new SqlParameter("@username", GlobalUsername);
                ReqCom1.Parameters.Add(ReqName1);



                con.Open();
                SqlDataReader drreq1;

                drreq1 = ReqCom1.ExecuteReader();

                if (drreq1.Read())
                {
                    request1 = drreq1["REQUEST1"].ToString();
                }
                con.Close();

               
                    sub1 = LabelInvite1.Text.Substring(14, 4);
                    intNo1 = Convert.ToInt32(sub1);
                    SqlCommand Req1 = new SqlCommand("spReInvite1", con);
                    Req1.CommandType = CommandType.StoredProcedure;

                    SqlParameter Para1 = new SqlParameter("@intNo1", intNo1);
                    Req1.Parameters.Add(Para1);



                    con.Open();
                    SqlDataReader dr1;

                    dr1 = Req1.ExecuteReader();

                    if (dr1.Read())
                    {
                        Interviewer1 = dr1["INTERVIEWER1"].ToString();
                        Interviewer2 = dr1["INTERVIEWER2"].ToString();
                        Interviewer3 = dr1["INTERVIEWER3"].ToString();
                        InterviewDate = dr1["INTERVIEWDATE"].ToString();
                    }
                    con.Close();

                    Session["Interviewer1"] = Interviewer1;
                    Session["Interviewer2"] = Interviewer2;
                    Session["Interviewer3"] = Interviewer3;
                    Session["InterviewNo"] = intNo1;
                    Session["IntervieweDate"] = InterviewDate;
                    Session["Sender1"] = Sender1;



            }
            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg1", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();
                con.Close();
            }
            Response.Redirect("DivAdminReInvite.aspx");
        }
        protected void ButtonReply2_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {

                SqlCommand ReqCom1 = new SqlCommand("spRequestchecker2", con);
                ReqCom1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName1 = new SqlParameter("@username", GlobalUsername);
                ReqCom1.Parameters.Add(ReqName1);



                con.Open();
                SqlDataReader drreq1;

                drreq1 = ReqCom1.ExecuteReader();

                if (drreq1.Read())
                {
                    request2 = drreq1["REQUEST2"].ToString();
                }
                con.Close();


                sub2 = LabelInvite2.Text.Substring(14, 4);
                intNo2 = Convert.ToInt32(sub2);
                SqlCommand Req1 = new SqlCommand("spReInvite2", con);
                Req1.CommandType = CommandType.StoredProcedure;

                SqlParameter Para1 = new SqlParameter("@intNo2", intNo2);
                Req1.Parameters.Add(Para1);



                con.Open();
                SqlDataReader dr1;

                dr1 = Req1.ExecuteReader();

                if (dr1.Read())
                {
                    Interviewer1 = dr1["INTERVIEWER1"].ToString();
                    Interviewer2 = dr1["INTERVIEWER2"].ToString();
                    Interviewer3 = dr1["INTERVIEWER3"].ToString();
                    InterviewDate = dr1["INTERVIEWDATE"].ToString();
                }
                con.Close();

                Session["Interviewer1"] = Interviewer1;
                Session["Interviewer2"] = Interviewer2;
                Session["Interviewer3"] = Interviewer3;
                Session["InterviewNo"] = intNo2;
                Session["IntervieweDate"] = InterviewDate;
                Session["Sender2"] = Sender2;



            }
            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg2", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();
                con.Close();
            }
            Response.Redirect("DivAdminReInvite2.aspx");
        }
        protected void ButtonReply3_Click(object sender, EventArgs e)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {

                SqlCommand ReqCom1 = new SqlCommand("spRequestchecker3", con);
                ReqCom1.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName1 = new SqlParameter("@username", GlobalUsername);
                ReqCom1.Parameters.Add(ReqName1);



                con.Open();
                SqlDataReader drreq1;

                drreq1 = ReqCom1.ExecuteReader();

                if (drreq1.Read())
                {
                    request3 = drreq1["REQUEST3"].ToString();
                }
                con.Close();


                sub3 = LabelInvite3.Text.Substring(14, 4);
                intNo3 = Convert.ToInt32(sub1);
                SqlCommand Req1 = new SqlCommand("spReInvite3", con);
                Req1.CommandType = CommandType.StoredProcedure;

                SqlParameter Para1 = new SqlParameter("@intNo3", intNo3);
                Req1.Parameters.Add(Para1);



                con.Open();
                SqlDataReader dr1;

                dr1 = Req1.ExecuteReader();

                if (dr1.Read())
                {
                    Interviewer1 = dr1["INTERVIEWER1"].ToString();
                    Interviewer2 = dr1["INTERVIEWER2"].ToString();
                    Interviewer3 = dr1["INTERVIEWER3"].ToString();
                    InterviewDate = dr1["INTERVIEWDATE"].ToString();
                }
                con.Close();

                Session["Interviewer1"] = Interviewer1;
                Session["Interviewer2"] = Interviewer2;
                Session["Interviewer3"] = Interviewer3;
                Session["InterviewNo"] = intNo3;
                Session["IntervieweDate"] = InterviewDate;
                Session["Sender3"] = Sender3;



            }
            using (SqlConnection con = new SqlConnection(CS))
            {



                SqlCommand com = new SqlCommand("spDeleteMsg3", con);
                com.CommandType = CommandType.StoredProcedure;

                SqlParameter sendname1 = new SqlParameter("@username", GlobalUsername);
                com.Parameters.Add(sendname1);



                con.Open();
                SqlDataReader dr;

                dr = com.ExecuteReader();
                con.Close();
            }
            Response.Redirect("DivAdminReInvite3.aspx");
        }




    

        
    }
}